package com.itbaizhan.jsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class Demo2 {
    //Jsoup的使用
    /*解析本地文本：static Document parse(File in,String charsetName)
    * 解析html或xml字符串  static Document parse(String html)
      解析网页源文件：static Document parse(URL url,int timeoutMillis)*/

    public static void main(String[] args) throws IOException {
        //解析本地xml
        ClassLoader classLoader = Demo2.class.getClassLoader();
        String path = classLoader.getResource("com/itbaizhan/xsd/student01.xml").getPath();
        //String path= Demo2.class.getClassLoader().getResource("com/itbaizhan/xsd/student01.xml").getPath();
       //获取文件对象
        Document document= Jsoup.parse(new File(path),"utf-8");
        System.out.println(document);
        System.out.println("-----------------");

        //解析字符串
        Document document1 = Jsoup.parse("<?xml version=\"1.0\" ?>\n" +
                "<students\n" +
                "        xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                "        xsi:schemaLocation=\"http://www.itbaizhan.cn/xml student.xsd\"\n" +
                "        xmlns:s=\"http://www.itbaizhan.cn/xml\">\n" +
                "    <!--s 是别名（可以任意起名）-->\n" +
                "    <s:student number=\"baizhan_0001\">\n" +
                "        <name>bai</name>\n" +
                "        <age>10</age>\n" +
                "        <sex>male</sex>\n" +
                "    </s:student>\n" +
                "\n" +
                "    <s:student number=\"baizhan_0002\">\n" +
                "        <name>sxt</name>\n" +
                "        <age>12</age>\n" +
                "        <sex>female</sex>\n" +
                "    </s:student>\n" +
                "</students>");

        System.out.println(document1);
        System.out.println("-------解析网络资源--------");
        Document document2 = Jsoup.parse(new URL("https://www.baidu.com"),3000);
        System.out.println(document2);
    }
}
